//
//  PywCommonDataDealUtils.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2017/8/10.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonDataDealUtils.h"
#import "PywCommonEncryptUtils.h"

@implementation PywCommonDataDealUtils

+(NSData*)encode:(NSData*)data
{
    NSData *encrypData = [PywCommonEncryptUtils gzipData:data];
    
    long length = [encrypData length];
    Byte *tmpBuf = (Byte*)[encrypData bytes];
    Byte *result = (Byte*)malloc(length);
    for (long i = 0, j = 0, k = length -1; i<length;) {
        result[j++] = tmpBuf[i++];
        if (i == length)
            break;
        result[k--] = tmpBuf[i++];
    }
    
    NSData *resultData = [NSData dataWithBytes:result length:length];
    return resultData;
}

+(NSData*)decode:(NSData*) data
{
    long length = [data length];
    
    Byte *tmpBuf = (Byte*)[data bytes];
    
    Byte *result = (Byte*)malloc(length);
    for (long i = 0, j = length - 1, k = 0;; i++, j--) {
        if (i == j) {
            result[k] = tmpBuf[i];
            break;
        }else if (i>j){
            break;
        }
        result[k++] = tmpBuf[i];
        result[k++] = tmpBuf[j];
    }
    
    NSData *resultData = [NSData dataWithBytes:result length:length];
    NSData *declipherData = [PywCommonEncryptUtils uncompressZippedData:resultData];
    return declipherData;
}

@end
